﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace DotNet.WebUI.Components
{
    public partial class JsonTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnJSON_Click(object sender, EventArgs e)
        {
            Student sdudent = new Student();
            sdudent.ID = 1;
            sdudent.Name = "丁勇";
            sdudent.NickName = "DingY";
            sdudent.Class = new Class() { Name = "CS0216", ID = 0216 };

            //实体序列化和反序列化
            string json1 = JsonHelper.SerializeObject(sdudent);
            txtResult.Text = json1;
            Student sdudent1 = JsonHelper.DeserializeJsonToObject<Student>(json1);

            //实体集合序列化和反序列化
            List<Student> sdudentList = new List<Student>() { sdudent, sdudent1 };
            string json2 = JsonHelper.SerializeObject(sdudentList);
            txtResult.Text += "\n" + json2;

            //DataTable序列化和反序列化
            DataTable dt = new DataTable();
            dt.TableName = "Student";
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name");
            dt.Columns.Add("NickName");
            DataRow dr = dt.NewRow();
            dr["ID"] = 112;
            dr["Name"] = "丁勇";
            dr["NickName"] = "DingY";
            dt.Rows.Add(dr);
            string json3 = JsonHelper.SerializeObject(dt);
            txtResult.Text += "\n" + json3;

            DataTable sdudentDt3 = JsonHelper.DeserializeJsonToObject<DataTable>(json3);
        }

        /// <summary>
        /// 学生信息实体
        /// </summary>
        public class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public string NickName { get; set; }
            public Class Class { get; set; }
        }

        /// <summary>
        /// 学生班级实体
        /// </summary>
        public class Class
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
    }
}